﻿namespace Raven
{
    public class GoalDodgeSideToSide : Goal<RavenBot>
    {
        private Vector2    _strafeTarget;

        private bool        _clockwise;

        private Vector2 GetStrafeTarget()
        {
            return _strafeTarget;
        }
            
        public GoalDodgeSideToSide(RavenBot owner) : base(owner, GoalType.goal_strafe)
        {
            _clockwise = _owner.GetWorld().GetRandom().RandBool();
        }

        public override void Activate()
        {
            _status = GoalStatus.active;

            _owner.GetSteering().SeekOn();

  
            if (_clockwise)
            {
                if (_owner.CanStepRight(ref _strafeTarget))
                {
                    _owner.GetSteering().SetTarget(_strafeTarget);
                }
                else
                {
                    //debug_con << "changing" << "";
                    _clockwise = !_clockwise;
                    _status = GoalStatus.inactive;
                }
            }

            else
            {
                if (_owner.CanStepLeft(ref _strafeTarget))
                {
                    _owner.GetSteering().SetTarget(_strafeTarget);
                }
                else
                {
                    // debug_con << "changing" << "";
                    _clockwise = !_clockwise;
                    _status = GoalStatus.inactive;
                }
            }
        }

        public override GoalStatus Process()
        {
            //if status is inactive, call Activate()
            ActivateIfInactive(); 

            //if target goes out of view terminate
            if (!_owner.GetTargetSys().isTargetWithinFOV())
            {
                _status = GoalStatus.completed;
            }

            //else if bot reaches the target position set status to inactive so the goal 
            //is reactivated on the next update-step
            else if (_owner.isAtPosition(_strafeTarget))
            {
                _status = GoalStatus.inactive;
            }

            return _status;
        }

        public override void Terminate()
        {
            _owner.GetSteering().SeekOff();
        }
    }
}